package yb.ecp.fast.user.web;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.util.Ref;
import yb.ecp.fast.infra.util.StringUtil;
import yb.ecp.fast.user.infra.AuthCode;
import yb.ecp.fast.user.infra.BasicController;
import yb.ecp.fast.user.infra.ErrorCode;
import yb.ecp.fast.user.service.DepartmentService;
import yb.ecp.fast.user.service.UserContextService;
import yb.ecp.fast.user.service.VO.DepartmentVO;

import java.util.ArrayList;
import java.util.List;

/**
 * 组织机构
 * Created by Li Changjiang on 2017/12/26 0026.
 */
@RequestMapping("/department")
@RestController
public class DepartmentController extends BasicController
{
    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private FastGenClient fastGenClient;

    @Autowired
   private  UserContextService userContextService;

    /**
     * 根据上级ID查询下级部门集合
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestParam("id") String id, @RequestHeader(name = "x-user-id", required = false) String userId) {
        if (StringUtils.isBlank(userId)) {
            return actionResult(ErrorCode.NeedLogin);
        }
        return actionResult(departmentService.listDept(id, userId));
    }

    /**
     * 查询部门详细信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/item", method = RequestMethod.GET)
    public ActionResult item(@RequestParam("id") String id)
    {
        return actionResult(departmentService.item(id));
    }


    /**
     * 添加一个新的部门
     *
     * @param departmentVO
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @FastMappingInfo(code = AuthCode.Department.INSERT)
    public ActionResult insert(@RequestBody DepartmentVO departmentVO, @RequestHeader(name = "x-user-id", required = false) String userId) {
        if (StringUtils.isBlank(userId)) {
            return actionResult(ErrorCode.NeedLogin);
        }
        String workspaceId = userContextService.getWorkspaceId(userId);

        if (StringUtil.isNullOrEmpty(workspaceId)) {
            return actionResult(ErrorCode.NeedLogin);
        }

        //获取ID
        ActionResult<String> textResult = fastGenClient.textGuid();
        if (textResult.getCode() != ErrorCode.Success.getCode()) {
            return textResult;
        }
        departmentVO.setId(textResult.getValue());
        ErrorCode ret = departmentService.insert(departmentVO,workspaceId);

        if (ErrorCode.Success == ret)
        {
            return actionResult(ret, textResult.getValue());
        }

        return actionResult(ret);
    }

    /**
     * 修改部门信息
     *
     * @param departmentVO
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @FastMappingInfo(code = AuthCode.Department.UPDATE)
    public ActionResult update(@RequestBody DepartmentVO departmentVO)
    {
        return actionResult(departmentService.update(departmentVO));
    }

    /**
     * 删除
     *
     * @param departmentVO DepartmentVO
     * @return ActionResult
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @FastMappingInfo(code = AuthCode.Department.REMOVE)
    public ActionResult delete(@RequestBody DepartmentVO departmentVO, @RequestHeader(name = "x-user-id", required = false) String userId) {
        if (StringUtils.isBlank(userId)) {
            return actionResult(ErrorCode.NeedLogin);
        }
        String workspaceId = userContextService.getWorkspaceId(userId);

        if (StringUtil.isNullOrEmpty(workspaceId)) {
            return actionResult(ErrorCode.NeedLogin);
        }

        Ref<List<String>> deptNameRef = new Ref<>(new ArrayList());

        return actionResult(departmentService.removeByCode(departmentVO.getId(), workspaceId, deptNameRef), deptNameRef.get());
    }

}
